/*
 * This is the listerner for the choose file button
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JList;

/**
 *
 * @author Nana
 */
public class ChooseFileListener implements ActionListener {

    private ChatGUI gui;

    private JList connectedUsers;

    /**
     * The constructor of the listener
     *
     * @param gui The GUI
     * @param connectedUsers The list of conneced useres
     */
    public ChooseFileListener(ChatGUI gui, JList connectedUsers) {
        this.gui = gui;
        this.connectedUsers = connectedUsers;
    }

    /**
     * The action to realize
     *
     * @param e The action event
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        File fichier;
        Path filePath;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            fichier = chooser.getSelectedFile();
            filePath = fichier.toPath();
            List selection = connectedUsers.getSelectedValuesList();
            ArrayList<String> to = new ArrayList<String>(selection.size());
            to.addAll(selection);
            gui.performSendFileProposal(filePath, to);
            System.out.println(chooser.getSelectedFile().getAbsolutePath());
        }
    }
}
